package behavior.observer;

import java.util.ArrayList;
import java.util.List;

public class WeatherData implements Subject {
    List<Observer> observerList = new ArrayList<Observer>();
    private float temperature;

    public WeatherData(float temperature) {
        this.temperature = temperature;
    }

    //温度变化通知订阅者
    public void setTemperature(float temperature) {
        this.temperature = temperature;
        notifyObservers();
    }

    @Override
    public void register(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void remove(Observer observer) {
        observerList.remove(observer);
    }

    @Override
    public void notifyObservers() {
        for (Observer observer:observerList) {
            observer.update(temperature);
        }
    }
}
